package com.yu.client;

import com.yu.service.*;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import java.net.MalformedURLException;
import java.util.Collections;
import java.util.List;

/**
 * @author : wenchao.long
 * @date : Created in 2021/01/05 14:35 周二
 * @description :
 */
public class CcCodingClient {

    public static void main(String[] args) throws Exception {
        //testSoapClient();
        testClient();
    }

    public static void testSoapClient() throws MalformedURLException {
        String address = "http://localhost:8081/service/ccCoding?WSDL";
        String nameSpaceUrl = "http://service.yu.com";
        String localPart = "CcCodingService";
        SoapClient soapClient = new SoapClient(address, nameSpaceUrl, localPart);

        soapClient.setUsername("test");
        soapClient.setPassword("test");

        CcCodingService port = soapClient.creatServiceInstance(CcCodingService.class);
        String result = port.saveCcCoding(getCcCodingDTO());

        String value = port.getAttributeValue("long");
        System.out.println("返回结果：" + result + "-----" + value);
    }

    public static void testClient() {
        CcCodingService_Service ccCodingService_service = new CcCodingService_Service();
        CcCodingService ccCodingServiceImplPort = ccCodingService_service.getCcCodingServiceImplPort();
        String value = ccCodingServiceImplPort.getAttributeValue("long");
        System.out.println("返回结果：" + "-----" + value);
    }

    public static void jaxWsProxyFactoryBean() {
        try {
            // 接口地址
            String address = "http://localhost:8081/service/ccCoding?WSDL";
            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            jaxWsProxyFactoryBean.setUsername("admin");
            jaxWsProxyFactoryBean.setPassword("admin");
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(CcCodingService.class);
            // 创建一个代理接口实现
            CcCodingService ccCodingService = (CcCodingService) jaxWsProxyFactoryBean.create();
            // 调用代理接口的方法调用并返回接口
            String result = ccCodingService.saveCcCoding(getCcCodingDTO());
            System.out.println("返回结果：" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<CcCodingDTO> getCcCodingDTO() {
        CcCodingDTO ccCodingDTO = new CcCodingDTO();
        ccCodingDTO.setId("123");
        ccCodingDTO.setName("test");

        CcCodingDescDTO ccCodingDescDTO = new CcCodingDescDTO();
        ccCodingDescDTO.setCcId("123");
        ccCodingDescDTO.setLanguage("中文");
        List<CcCodingDescDTO> ccCodingDescList = Collections.singletonList(ccCodingDescDTO);


        CcCodingAttributeInfoDTO ccCodingAttributeInfoDTO = new CcCodingAttributeInfoDTO();
        ccCodingAttributeInfoDTO.setCcId("123");
        ccCodingAttributeInfoDTO.setAttributeName("test");
        List<CcCodingAttributeInfoDTO> ccCodingAttributeInfoList = Collections.singletonList(ccCodingAttributeInfoDTO);

        ccCodingDTO.getCcCodingDescList().addAll(ccCodingDescList);
        ccCodingDTO.getCcCodingAttributeInfoList().addAll(ccCodingAttributeInfoList);

        return Collections.singletonList(ccCodingDTO);
    }
}
